Imports SchemaCodeGen

Partial Class pages_TemplateSets_TemplateSet : Inherits CPage

#Region "Querystring"
	Public ReadOnly Property SetId() As Integer
		Get
			Return CWeb.RequestInt("setId")
		End Get
	End Property
	Public ReadOnly Property IsEdit() As Boolean
		Get
			Return SetId <> Integer.MinValue
		End Get
	End Property
#End Region

#Region "Members"
	Private m_templateSet As CTemplateSet
#End Region

#Region "Data"
	Public ReadOnly Property [TemplateSet]() As CTemplateSet
		Get
			If IsNothing(m_templateSet) Then
				If IsEdit Then
					m_templateSet = CTemplateSet.Cache.GetById(SetId)
					If IsNothing(m_templateSet) Then CSitemap.RecordNotFound("TemplateSet", SetId)
				Else
					m_templateSet = New CTemplateSet
				End If
			End If
			Return m_templateSet
		End Get
	End Property
#End Region

#Region "Navigation"
	Private Sub Refresh()
		Response.Redirect(CSitemap.TemplateSetEdit(Me.TemplateSet.SetId))
	End Sub
	Private Sub ReturnToList()
		Response.Redirect(CSitemap.TemplateSets)
	End Sub
#End Region

#Region "Event Handlers - Page"
	Protected Overrides Sub PageInit()
		With ddSetLanguageId
			.DataTextField = "LanguageName"
			.DataValueField = "LanguageId"
			.DataSource = CLanguage.Cache
			.DataBind()
		End With


		With ddSetParentSetId
			.DataTextField = "SetName"
			.DataValueField = "SetId"
			.DataSource = CTemplateSet.Cache.Except(TemplateSet)
			.DataBind()
			.BlankItem("-- Select Parent --")
		End With



		'Page Title
		If IsEdit Then
			Me.Title = "Pattern Details"
			ctrlTemplateSets.DisplaySelected(TemplateSet)
			ctrlTempatesMissing.DisplayRemaining(TemplateSet)
		Else
			Me.PageTitle = "Create New Pattern"
			txtSetCreated.Visible = False
			txtSetUserName.Visible = False
			ctrlTemplateSets.Visible = False
			ctrlTempatesMissing.Visible = False
		End If

		'Textbox logic
		'Me.Form.DefaultFocus = txtTemplateSetName.Textbox.ClientID;
		Me.Form.DefaultButton = btnSave.UniqueID   'txtTemplateSetName.OnReturnPress(btnSave)

		'Button Text
		btnDelete.Visible = IsEdit
		If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Pattern"
		If IsEdit Then AddButton(CSitemap.TemplateSetAdd(), "Create a new Pattern")
	End Sub
	Protected Overrides Sub PageLoad()
		If Page.IsPostBack Then Exit Sub

		LoadTemplateSet()
	End Sub
	Protected Overrides Sub OnPreRender(e As EventArgs)
		If IsEdit Then
			AddMenuSide("New Pattern...", CSitemap.TemplateSetAdd)
		End If
	End Sub
#End Region

#Region "Event Handlers - Form"
	Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If Not Me.IsValid() Then Exit Sub
		SaveTemplateSet()
		Refresh()
	End Sub
	Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
		ReturnToList()
	End Sub
	Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
		Me.TemplateSet.Delete()
		'CCache.ClearCache()
		ReturnToList()
	End Sub
#End Region

#Region "Private - Load/Save"
	Protected Sub LoadTemplateSet()
		With Me.TemplateSet
			txtSetName.Text = .SetName
			ddSetLanguageId.ValueInt = .SetLanguageId
			ddSetParentSetId.ValueInt = .SetParentSetId
			ddSetParentSetId.Visible = .SetParentSetId > 0
			txtSetUserName.Text = .SetUserName
			txtSetCreated.Value = CUtilities.Timespan(.SetCreated)
			If IsEdit Then
				ddSetLanguageId.Mode = EControlMode.Locked
			End If
			If IsNothing(.SetUserName) Then
				txtSetUserName.Mode = EControlMode.Locked
				txtSetUserName.Value = "*Shared"
			End If
			If IsNothing(.SetName) Then
				txtSetName.Mode = EControlMode.Locked
				txtSetName.Text = "*Default"
			End If
		End With
	End Sub
	Protected Sub SaveTemplateSet()
		With Me.TemplateSet
			If Not IsEdit Then .SetUserName = CSession.User.UserLoginName
			If Not IsEdit Then .SetParentSetId = ddSetParentSetId.ValueInt

			.SetName = txtSetName.Text
			.SetLanguageId = ddSetLanguageId.ValueInt

			.Save()
		End With
		'CCache.ClearCache()
	End Sub
#End Region

End Class
